import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split, KFold, StratifiedKFold, GroupKFold, TimeSeriesSplit
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# 创建示例数据集
X, y = make_classification(
    n_samples=1000,  # 1000个样本
    n_features=20,   # 20个特征
    n_classes=3,     # 3个类别
    n_clusters_per_class=1,
    random_state=42
)

# 添加时间戳和组ID用于特定分割策略
timestamps = pd.date_range(start='2023-01-01', periods=1000, freq='D')
group_ids = np.repeat(np.arange(100), 10)  # 100个组，每组10个样本


# 简单随机分割（70%训练，15%验证，15%测试）
X_temp, X_test, y_temp, y_test = train_test_split(
    X, y, test_size=0.15, random_state=42, stratify=y
)
X_train, X_val, y_train, y_val = train_test_split(
    X_temp, y_temp, test_size=0.1765, random_state=42, stratify=y_temp  # 0.15/(1-0.15)≈0.1765
)

print(f"随机分割结果: 训练集={X_train.shape[0]}, 验证集={X_val.shape[0]}, 测试集={X_test.shape[0]}")

# K折交叉验证（K=5）
k = 5
kf = KFold(n_splits=k, shuffle=True, random_state=42)
fold_scores = []

for fold, (train_index, val_index) in enumerate(kf.split(X)):
    print(f"\nFold {fold + 1}/{k}")
    X_train, X_val = X[train_index], X[val_index]
    y_train, y_val = y[train_index], y[val_index]

    # 训练模型（这里使用随机森林作为示例）
    model = RandomForestClassifier(n_estimators=100, random_state=42)
    model.fit(X_train, y_train)

    # 验证集评估
    val_pred = model.predict(X_val)
    acc = accuracy_score(y_val, val_pred)
    fold_scores.append(acc)
    print(f"验证集准确率: {acc:.4f}")

print(f"\n平均准确率: {np.mean(fold_scores):.4f} ± {np.std(fold_scores):.4f}")